package com.qs.questionnairesurveysystem.controller;

import com.qs.questionnairesurveysystem.dto.Result;
import com.qs.questionnairesurveysystem.entity.Answer;
import com.qs.questionnairesurveysystem.service.AnswerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/answers")
@Api(tags = "Answer Management", description = "Operations related to managing answers")
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @ApiOperation(value = "Create a new answer", notes = "Create a new answer for a survey.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Answer created successfully"),
            @ApiResponse(code = 400, message = "Invalid input")
    })
    @PostMapping
    public Result<String> createAnswer(@RequestBody Answer answer) {
        answerService.createAnswer(answer);
        return Result.success("Answer created successfully.");
    }

    @ApiOperation(value = "Update an answer", notes = "Update an existing answer by ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Answer updated successfully"),
            @ApiResponse(code = 400, message = "Invalid input"),
            @ApiResponse(code = 404, message = "Answer not found")
    })
    @PutMapping("/{answerId}")
    public Result<String> updateAnswer(@PathVariable Long answerId, @RequestBody Answer answer) {
        answer.setAnswerId(answerId);
        answerService.updateAnswer(answer);
        return Result.success("Answer updated successfully.");
    }

    @ApiOperation(value = "Delete an answer", notes = "Delete an answer by ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Answer deleted successfully"),
            @ApiResponse(code = 404, message = "Answer not found")
    })
    @DeleteMapping("/{answerId}")
    public Result<String> deleteAnswer(@PathVariable Long answerId) {
        answerService.deleteAnswer(answerId);
        return Result.success("Answer deleted successfully.");
    }

    @ApiOperation(value = "Get an answer by ID", notes = "Retrieve a specific answer by ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved answer"),
            @ApiResponse(code = 404, message = "Answer not found")
    })
    @GetMapping("/{answerId}")
    public Result<Answer> getAnswerById(@PathVariable Long answerId) {
        Answer answer = answerService.getAnswerById(answerId);
        return answer != null ? Result.success(answer) : Result.error("not found",404);
    }

    @ApiOperation(value = "Get all answers", notes = "Retrieve a list of all answers.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved list of answers")
    })
    @GetMapping
    public ResponseEntity<List<Answer>> getAllAnswers() {
        List<Answer> answers = answerService.getAllAnswers();
        return ResponseEntity.ok(answers);
    }

    @ApiOperation(value = "Get answers by survey ID", notes = "Retrieve all answers for a specific survey.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved answers"),
            @ApiResponse(code = 404, message = "Survey not found")
    })
    @GetMapping("/survey/{surveyId}/{uid}")
    public Result<List<Answer>> getAnswersBySurveyId(@PathVariable Long surveyId,@PathVariable Long uid) {
        List<Answer> answers = answerService.getAnswersBySurveyIdUid(surveyId,uid);

        return Result.success(answers);
    }

    @ApiOperation(value = "Get answers by question ID", notes = "Retrieve all answers for a specific question.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved answers"),
            @ApiResponse(code = 404, message = "Question not found")
    })
    @GetMapping("/question/{questionId}")
    public Result<List<Answer>> getAnswersByQuestionId(@PathVariable Long questionId) {
        List<Answer> answers = answerService.getAnswersByQuestionId(questionId);
        return Result.success(answers);
    }
}
